package com.uchonor.api.core.service;

import com.uchonor.api.core.entity.InterfaceGroup;
import com.uchonor.api.core.mapper.InterfaceGroupMapper;
import com.uchonor.olight.agent.annotation.Inject;
import com.uchonor.olight.agent.annotation.Service;
import com.uchonor.olight.agent.annotation.db.DBTransactional;
import com.uchonor.olight.agent.module.db.BaseServiceImpl;
import org.ssssssss.magicapi.core.model.Group;

@Service
public class InterfaceGroupService extends BaseServiceImpl<InterfaceGroupMapper, InterfaceGroup> {
    @Inject
    private ResourceService resourceService;

    @DBTransactional
    public String saveInterfaceGroup(InterfaceGroup interfaceGroup){
        this.save(interfaceGroup);
        Group group = new Group();
        group.setId(interfaceGroup.getGroupId());
        group.setName(interfaceGroup.getGroupName());
        group.setType(interfaceGroup.getGroupType());
        group.setParentId(interfaceGroup.getGroupParentId());
        group.setPath(interfaceGroup.getGroupPath());
        group.setCreateBy(interfaceGroup.getCreateUser());
        group.setUpdateBy(interfaceGroup.getUpdateUser());
        group.setCreateTime(interfaceGroup.getCreateTime()!=null?interfaceGroup.getCreateTime().getTime():null);
        group.setUpdateTime(interfaceGroup.getUpdateTime()!=null?interfaceGroup.getUpdateTime().getTime():null);
        resourceService.saveGroup(group);
        return interfaceGroup.getGroupId();
    }

    @DBTransactional
    public void deleteInterfaceGroup(String groupId){
        this.removeById(groupId);
        resourceService.delete(groupId);
    }
}
